/**
 * Created by Windows on 2015/11/18.
 */
//首页头部搜索的板块
$(function(){

    $(".search .search_tty span").on("click",function(){
        $(this).addClass("curr").siblings().removeClass("curr");
    });

    $("#search_submit").on("click",function(){
        var vid = $(".search .search_tty .curr").attr("v");
        var search_key = $("#search_key").val();
        if(!search_key || search_key==''){
            alert("请填写搜索内容!");return false;
        }
        //这边过滤无意义的字符
        search_key.replace('%','\%').replace('_','\_');
        var param;
        param = {
            'kd':search_key
        }

        var url = encodeURI("/search/index?kd="+search_key);
        if(vid ==1){//现货
            window.location.href=url;
        }

        if(vid==2){//求购
            window.location.href=encodeURI("/buy/index?kd="+search_key);
        }

    });

});

$(function(){
    $(document).delegate('.product_list_title .operate,.product_list_title .icons',"click", function(e) {
        e.preventDefault();
        if(!$(this).closest('.product_item').hasClass('curr')){

            $(this).closest('.product_item').removeClass('curr');
            $(this).removeClass('curr');
            $(this).siblings('.icons').removeClass('curr');
            $(this).find('a').text('查看');
            $(this).siblings('.operate').find('a').text('查看');



        }else{
            var product_id = $(this).attr("product_id");
            var obj = $(this);
            $.ajax({
                type: 'GET',
                url: '/search/productjson',
                data: {
                    product_id: product_id,
                },
                dataType: 'text',
                success: function(json) {
                    var json = $.parseJSON(json);
                    var  li = "";
                    for(list_index in json) {
                        li+="<li>";
                        li+="<div class=\"plc_items\">";
                        if(json[list_index]['type']=='stock'){
                            li+="<div class=\"items plc_items_text\"><i class=\"plc_icon\">自营现货</i>";
                        }else{
                            li+="<div class=\"items plc_items_text\">"+json[list_index]['business_name'];
                        }
                        li+="<p class=\"plc_text\"><span class=\"pr5\">"+json[list_index]['contacts']+"</span><span class=\"pr5\">"+json[list_index]['mobile']+"</span><span class=\"pr5\"><a target='_blank' href=\"http://wpa.qq.com/msgrd?v=3&uin="+json[list_index]['qq']+"&site=qq&menu=yes\"><img src=\"/images/login_qq.png\" class=\"vam mt_3\" /></a></span><span class=\"green\">"+json[list_index]['delivery_time']+"</span></p>";
                        li+="</div>";
                        li+="<span class=\"items firm\">"+json[list_index]['area_name']+"</span>";

                        if(list_index==0){
                            li+="<span class=\"items price blue fwb\">"+json[list_index]['price']+"<i>最低</i></span>";
                        }else{
                            li+="<span class=\"items price blue fwb\">"+json[list_index]['price']+"</span>";
                        }

                        li+="<span class=\"items number\">"+json[list_index]['num']+"</span>";
                        li+="<span class=\"items time\">"+json[list_index]['update_time']+"</span>";
                        li+="<span class=\"items operate\">";
                        li+="<a href=\"javascript:void(0)\" buy_id=\""+json[list_index]['id']+"\" class=\"buy\">立即认购</a><a target='_blank' href=\"http://wpa.qq.com/msgrd?v=3&uin="+json[list_index]['qq']+"&site=qq&menu=yes\" class=\"negotiate\">委托洽谈</a>";
                        li+="</span>";
                        li+="</div>";
                        li+="</li>";
                    }

                    //$(this).closest(".product_list_title").siblings(".product_list_content").html(li);
                    obj.closest(".product_list_title").siblings(".product_list_content").html(li);
                }

            });

            $('.product_item').closest('.product_item').removeClass('curr');
            $('.product_item').removeClass('curr');
            $('.product_item').siblings('.icons').removeClass('curr');
            $('.product_item').find('a').text('查看');
            $('.product_item').siblings('.operate').find('a').text('查看');

            $(this).closest('.product_item').addClass('curr');
            $(this).addClass('curr');
            $(this).siblings('.icons').addClass('curr');
            $(this).find('a').text('收起');
            $(this).siblings('.operate').find('a').text('收起');

        }
    });
});
